//
//  MyViewController.m
//  BaseFrame
//
//  Created by 孟学浩 on 2017/4/1.
//  Copyright © 2017年 com.mxh. All rights reserved.
//

#import "MyViewController.h"
#import "MyTableViewCell.h"
#import "AgentsViewController.h"
#import "OrderListViewController.h"
#import "JPUSHService.h"

#import "CollectViewController.h"
#import "DealViewController.h"
#import "SystemMsgViewController.h"
#import "SetupViewController.h"
#import "ComplainViewController.h"
#import "MyReceiveAddressViewController.h"
#import "BuyCarViewController.h"
#import "MyMembershipCardViewController.h"
#import "ContactCustomerServiceViewController.h"
#import "LoginViewController.h"
#import "MyInformationViewController.h"
#import "MakeAnAppointmentToDoLaundryViewController.h"
#import "DudsViewController.h"
@interface MyViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) UIView *headerView;
@property (nonatomic,strong) UIImageView *imgView;
@property (nonatomic,strong) UILabel *userNameLabel;
Strong UIView *footView;
@property (nonatomic,strong) NSArray *dataArray;

@end

//网络
@implementation MyViewController (NetWorking)
- (void)viewCommodityInformation{
    [self showprogressHUD];
    NSDictionary *parameters = @{@"longitude":[UserModel sharedInstanced].longitude,
                                 @"latitude":[UserModel sharedInstanced].latitude};
    [[AFAppDotNetAPIClient sharedClient] POST:@"wardrobe/Mobile_Phone_Terminal/viewCommodityInformation.do" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenProgressHUD];
        if ([responseObject[@"responseStatus"] isEqualToString:@"0"]) {
            MakeAnAppointmentToDoLaundryViewController *vc = [MakeAnAppointmentToDoLaundryViewController new];
            vc.hidesBottomBarWhenPushed = YES;
            vc.aId = responseObject[@"obj"][0][@"a_id"];
            [self.navigationController pushViewController:vc animated:YES];
        }else{
            ShowMessage(responseObject[@"msg"]);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self hiddenProgressHUD];
        [self showError:error];
    }];
}


@end

//UI
@implementation MyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"我的";
    [self.view addSubview:self.tableView];
}

- (void)nTestViewDidClickBtn{
    [self viewCommodityInformation];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(nTestViewDidClickBtn) name:@"预约洗衣" object:nil];
    [self.imgView sd_setImageWithURL:[NSURL URLOffsetIPWithString:[UserModel sharedInstanced].headImg]];
    self.userNameLabel.attributedText = [self getAttributeName:[UserModel sharedInstanced].nameNick info:[UserModel sharedInstanced].mobile];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - click

- (void)tableHeadViewCkick{
    MyInformationViewController *vc = [MyInformationViewController new];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)exitLoginButtonClick{
    [UserModel sharedInstanced].userId = @"";
    LoginViewController *vc = [LoginViewController new];
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc];
    vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentViewController:nav animated:YES completion:nil];
}

#pragma mark - UITableViewDelegate & UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MyTableViewCell *cell = [MyTableViewCell cellForTableView:tableView];
    cell.model = self.dataArray[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44 / WIDTH_5S_SCALE;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.row) {
        case 0:{
            MyMembershipCardViewController *vc = [MyMembershipCardViewController new];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }break;
        case 1:{
            BuyCarViewController *vc = [BuyCarViewController new];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }break;
        case 2:{
            MyReceiveAddressViewController *vc = [MyReceiveAddressViewController new];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }break;
        case 5:{
            ContactCustomerServiceViewController *vc = [ContactCustomerServiceViewController new];
            vc.nsTitle = @"投诉建议";
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }break;
        case 6:{
            DudsViewController *vc = [[DudsViewController alloc] init];
            vc.title = @"旧衣回收";
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }break;
        case 7:{
            ContactCustomerServiceViewController *vc = [ContactCustomerServiceViewController new];
            vc.nsTitle = @"联系我们";
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }break;
        case 8:{
            [self exitLoginButtonClick];
        }
            default:
            break;
    }
}

#pragma mark - set & get

- (UIView *)footView{
    if (!_footView) {
        _footView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 80 / WIDTH_5S_SCALE)];
        UIButton *exitLoginButton = [[UIButton alloc]initWithFrame:CGRectMake(50 / WIDTH_5S_SCALE, 50 / WIDTH_5S_SCALE,SCREEN_WIDTH - 100 / WIDTH_5S_SCALE, 30 / WIDTH_5S_SCALE)];
        exitLoginButton.layer.masksToBounds = YES;
        exitLoginButton.layer.cornerRadius = 5;
        exitLoginButton.titleLabel.font = DEF_FontSize_14;
        exitLoginButton.backgroundColor = getColor(mainColor);
        [exitLoginButton setTitle:@"退出登录" forState:UIControlStateNormal];
        [exitLoginButton setTitleColor:getColor(@"ffffff") forState:UIControlStateNormal];
        [exitLoginButton addTarget:self action:@selector(exitLoginButtonClick) forControlEvents:UIControlEventTouchUpInside];
        exitLoginButton.hidden = YES;
        [_footView addSubview:exitLoginButton];
    }
    return _footView;
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.sectionFooterHeight = .01;
        _tableView.sectionHeaderHeight = .01;
        _tableView.tableFooterView = self.footView;
        _tableView.tableHeaderView = self.headerView;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.backgroundColor = getColor(@"ffffff");
    }
    return _tableView;
}

- (UIView *)headerView{
    if (!_headerView) {
        _headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 66 / WIDTH_5S_SCALE + 5 / WIDTH_5S_SCALE)];
        _headerView.backgroundColor = getColor(@"eeeeee");
        UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 66 / WIDTH_5S_SCALE)];
        bgView.backgroundColor = getColor(@"ffffff");
        [_headerView addSubview:bgView];
        UIImageView *rightImageView = [[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH - 7 / WIDTH_5S_SCALE - 15 / WIDTH_5S_SCALE, 0, 7 / WIDTH_5S_SCALE, 66 / WIDTH_5S_SCALE)];
        rightImageView.contentMode = UIViewContentModeScaleAspectFit;
        rightImageView.image = [UIImage imageNamed:@"right_image"];
        [bgView sd_addSubviews:@[self.imgView , self.userNameLabel , rightImageView]];
        _headerView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tableHeadViewCkick)];
        [_headerView addGestureRecognizer:tap];
    }
    return _headerView;
}

- (UILabel *)userNameLabel{
    if (!_userNameLabel) {
        _userNameLabel  = [[UILabel alloc]initWithFrame:CGRectMake(66 / WIDTH_5S_SCALE, 0, SCREEN_WIDTH, 66 / WIDTH_5S_SCALE)];
        _userNameLabel.font = DEF_FontSize_14;
        _userNameLabel.textColor = getColor(textColor);
        _userNameLabel.numberOfLines = 0;
        _userNameLabel.attributedText = [self getAttributeName:[UserModel sharedInstanced].nameNick info:[UserModel sharedInstanced].mobile];
    }
    return _userNameLabel;
}

- (NSAttributedString *)getAttributeName:(NSString *)name info:(NSString *)info{
    NSMutableAttributedString *attStr = [NSMutableAttributedString new];
    
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:name]];
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n"]];
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:@"联系电话:"]];
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:info]];
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:6];//调整行间距
    
    [attStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [attStr.mutableString length])];
    
    return attStr;
}

- (UIImageView *)imgView{
    if (!_imgView) {
        _imgView = [[UIImageView alloc]initWithFrame:CGRectMake(10 / WIDTH_5S_SCALE, 10 / WIDTH_5S_SCALE, 46 / WIDTH_5S_SCALE, 46 / WIDTH_5S_SCALE)];
        _imgView.contentMode = UIViewContentModeScaleAspectFill;
        _imgView.backgroundColor = getColor(@"90cef8");
        _imgView.layer.masksToBounds = YES;
        _imgView.layer.cornerRadius = 23 / WIDTH_5S_SCALE;
    }
    return _imgView;
}

- (NSArray *)dataArray{
    if (!_dataArray) {
        _dataArray = @[
                       @{@"img":@"TheMembershipCard",@"title":@"会员卡",@"flag":@"1"},
                       @{@"img":@"TheShoppingCart",@"title":@"购物车",@"flag":@"1"},
                       @{@"img":@"ShippingAddress",@"title":@"收货地址",@"flag":@"1"},
                       @{@"img":@"lishidingdan",@"title":@"订历史订单",@"flag":@"1"},
                       @{@"img":@"dianpu",@"title":@"我的店铺",@"flag":@"1"},
                       @{@"img":@"ComplaintsSuggestions",@"title":@"投诉建议",@"flag":@"1"},
                       @{@"img":@"huishou-1",@"title":@"旧衣回收",@"flag":@"1"},
                       @{@"img":@"phone",@"title":@"联系我们",@"flag":@"1"},
                       @{@"img":@"tuichu",@"title":@"退出程序",@"flag":@"1"}];
    }
    return _dataArray;
}

@end
